import 'package:flutter/cupertino.dart';

class StringUtils {
  ///判断一个字符串是否是标准的登录密码
  ///  1. 长度大于8位
  ///  2. 密码中必须同时包含数字和字母
  static bool isStandardLoginPassword(String inputStr) {
    if (inputStr.length < 6) {
      return false;
    }
    String regex = "^(?![0-9]+\$)(?![a-zA-Z]+\$)[0-9A-Za-z]{6,18}\$";
    return RegExp(regex).hasMatch(inputStr);
  }

  /// 判断字符串是否符合浮点数
  static bool isFloatNum(String str) {
    try {
      double num = double.parse(str);
      return true;
    } catch (e) {
      return false;
    }
  }

  ///获取一个text的宽度
  static double getStringWidth(String str, double fontSize) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(text: str, style: TextStyle(fontSize: fontSize)),
        maxLines: 1,
        textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    // print(textPainter.size.width);
    return textPainter.size.width;
  }

  ///手机号中间打*
  static String getMiddleHideForMobile(String content) {
    if (content.length < 11) {
      return content;
    }
    String start = content.substring(0, 3);
    String end = content.substring(7);
    String mid = '****';

    return '$start$mid$end';
  }

  ///银行卡中间打*
  static String getMiddleHideForBankcard(String content) {
    if (content.length < 15) {
      return content;
    }
    String start = '***************';
    String end = content.substring(content.length - 4);

    return '$start$end';
  }

  ///返回银行卡号后四位
  static String getBankCardEndNum(String content) {
    if (content.length < 6) {
      return content;
    }
    String start = content.substring(content.length - 4);
    return start;
  }

  ///人名，后面用**
  static String getNameHide(String content) {
    if (content.length < 2) {
      return content;
    }
    StringBuffer start = StringBuffer(content.substring(0,1));
    for (int i = 1 ;i < content.length; i++) {
      start.write('*');
    }
    return start.toString();
  }

  ///邮箱中间打***
  static String getMiddleHideForEmail(String content) {

   int index= content.indexOf("@");
   if(index==-1){
     return content;
   }

   String start=content.substring(0,3);
   String end=content.substring(index,content.length);

   if(start.length>6){
     start=start.substring(0,6);
   }

    return '$start***$end';
  }
}
